package com.fightingFish.user.servlet;

import com.fightingFish.user.bean.Anchor;
import com.fightingFish.user.bean.Gift;
import com.fightingFish.user.bean.Record;
import com.fightingFish.user.bean.User;
import com.fightingFish.user.service.AttentionService;
import com.fightingFish.user.service.impl.AdministratorServiceImpl;
import com.fightingFish.user.service.impl.AnchorServiceImpl;
import com.fightingFish.user.service.impl.AttentionServiceImpl;
import com.fightingFish.user.service.impl.UserServiceImpl;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/12/18.
 */
@WebServlet("/anchorSearch")
public class AnchorSearchServlet extends HttpServlet {

    private AnchorServiceImpl asi = new AnchorServiceImpl();
    private AdministratorServiceImpl admin = new AdministratorServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置信息获取的编码
        req.setCharacterEncoding("utf-8");
        //2.设置返回文件的类型和编码
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //3.获取用户请求中的方法
        String[] methodNames = req.getParameterValues("methodName");
        //4.利用反射获取方法
        try {
            for (int i = 0; i < methodNames.length; i++) {
                Method method = this.getClass().getDeclaredMethod(methodNames[i], HttpServletRequest.class, HttpServletResponse.class);
                //5.调用该方法
                method.invoke(this, req, resp);
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据关键字查询主播
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryAnchorByKeyWords(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("queryAnchorByKeyWords");
        //获取关键字
        String keyWords = req.getParameter("keyWords");
        //调用service层方法获取查询到的主播
        List<Anchor> anchorList = asi.queryAnchorByKeyWords(keyWords);
        //将查询到的数据以json的方式返回给前台
        req.setAttribute("anchorList", anchorList);
        req.getRequestDispatcher("/jsp/Index.jsp").forward(req, resp);
    }

    /**
     * 获取当前登录用户的主播关注对象集合
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryAttentionAnchor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User u=(User)req.getSession().getAttribute("loginUser");
        //该用户关注的所有主播
        List<Anchor> attentionAnchorList = asi.queryAttentionAnchor(u.getUserId());
        req.getSession().setAttribute("AttentionList",attentionAnchorList);
        //该主播的所有粉丝
        List<User> selectAllFans=asi.selectAllByAnchor(u.getUserId());
        req.getSession().setAttribute("selectAllFans",selectAllFans);
        // 获取当前主播的个人信息
        User queryAnchorByRoomId=new UserServiceImpl().selectInfoByid(u.getUserId());
        req.getSession().setAttribute("queryAnchorByRoomId",queryAnchorByRoomId);
        // 获得所有的消费记录
        ArrayList<Record> arrayListRecord = new UserServiceImpl().selectRecordByid(u.getUserId());
        req.getSession().setAttribute("arrayListRecord",arrayListRecord);
       // 获得该主播的收入详情
        ArrayList<Record> recordsByAnchodId=new UserServiceImpl().selectRecordByAnchorId(u.getUserId());
        req.getSession().setAttribute("recordsByAnchodId",recordsByAnchodId);
        resp.sendRedirect(req.getContextPath()+"/jsp/PersonCenter.jsp");
    }

    /**
     * 根据主播房间号查询主播信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryAnchorByRoomId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int roomId = 0;
        try {
            roomId = Integer.parseInt(req.getParameter("roomId"));
        } catch (Exception e) {
        }
        Anchor queryAnchorByRoomId = asi.queryAnchorByRoomId(roomId);
        if (queryAnchorByRoomId != null) {
            req.setAttribute("queryAnchorByRoomId", queryAnchorByRoomId);
            req.getRequestDispatcher("/jsp/room.jsp").forward(req, resp);
        }
    }


    /**
     * 根据直播类型获取主播列表
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryAnchorByType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("queryAnchorByType");
        //定义用户直播类型
        int type1 = 15;//游戏
        int type2 = 16;//生活
        int type3 = 17;//科技
        int type4 = 18;//运动
        int type5 = 19;//动漫
        //调用service层方法获取该用户关注的主播

//        获取游戏类主播信息
        req.getServletContext().setAttribute("type1", asi.queryAnchorByType(type1));
//        获取生活类主播信息
        req.getServletContext().setAttribute("type2", asi.queryAnchorByType(type2));
//        获取科技类主播信息
        req.getServletContext().setAttribute("type3", asi.queryAnchorByType(type3));
//        获取运动类主播信息
        req.getServletContext().setAttribute("type4", asi.queryAnchorByType(type4));
//        获取动漫类主播信息
        req.getServletContext().setAttribute("type5", asi.queryAnchorByType(type5));
//        获取热度前4名主播的信息
        req.getServletContext().setAttribute("type6", asi.queryAnchorByAttention());
//        获取最新的主播 前4位
        req.getServletContext().setAttribute("type7", asi.queryAnchorByCreateTime());

        //获取所有的礼物信息并存入application域中
        List<Gift> giftList = admin.getGift();
        req.getServletContext().setAttribute("allGifts",giftList);

//        转发到主页
        RequestDispatcher rd = req.getRequestDispatcher("/jsp/Index.jsp");
        rd.forward(req, resp);
    }

    /**
     * 方法概述： 给用户推荐关注  自己未关注的粉丝量排行前4的主播
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void recommendAttention(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("recommendAttention");
        //获取当前登录账号信息
        User user = (User) req.getSession().getAttribute("loginUser");
        if (user == null) {
            //未登录
            resp.sendRedirect(req.getContextPath() + "jsp/login.jsp");
        } else {
            //已登录
            List<Anchor> recommendAttention = asi.recommendAttention(user.getUserId());
            req.getSession().setAttribute("recommendAttention", recommendAttention);
            req.getRequestDispatcher("/jsp/PersonCenter.jsp").forward(req,resp);
        }
    }
}
